package com.onlinelearn.paymentservice.controller;

import com.onlinelearn.paymentservice.dto.PaymentRequestDTO;
import com.onlinelearn.paymentservice.dto.RefundRequestDTO;
import com.onlinelearn.paymentservice.model.PaymentOrder;
import com.onlinelearn.paymentservice.service.PaymentOrderService;
import com.onlinelearn.paymentservice.service.RefundService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/api/payments")
public class PaymentController {
    @Autowired
    private PaymentOrderService paymentOrderService;

    @Autowired
    private RefundService refundService;

    @PostMapping("/create-order")
    public ResponseEntity<PaymentOrder> createOrder(
        @RequestParam Long studentId, 
        @RequestParam Long courseId
    ) {
        PaymentOrder order = paymentOrderService.createOrder(studentId, courseId);
        return ResponseEntity.ok(order);
    }

    @PostMapping("/process")
    public ResponseEntity<PaymentOrder> processPayment(
        @Valid @RequestBody PaymentRequestDTO request
    ) {
        PaymentOrder order = paymentOrderService.processPayment(
            request.getOrderId(), 
            request.getPaymentMethod()
        );
        return ResponseEntity.ok(order);
    }

    @PostMapping("/refund")
    public ResponseEntity<?> processRefund(
        @Valid @RequestBody RefundRequestDTO request
    ) {
        RefundResult result = refundService.processRefund(
            request.getOrderId(), 
            request.getReason()
        );
        return ResponseEntity.ok(result);
    }

    @GetMapping("/student-orders")
    public ResponseEntity<List<PaymentOrder>> getStudentOrders(
        @RequestParam Long studentId
    ) {
        List<PaymentOrder> orders = paymentOrderService.getStudentOrders(studentId);
        return ResponseEntity.ok(orders);
    }
} 